<template>
  <div>
      <!-- 搜索表单 -->
    <el-form ref="searcForm" :inline="true" :model="searchMap" class="demo-form-inline">
      <el-form-item prop="username" style="width: 200px">
        <el-input v-model="searchMap.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item prop="name" style="width: 200px">
        <el-input v-model="searchMap.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="feachData">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('searcForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 员工数据列表 -->
    <el-table :data="list" height="380" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="name" label="姓名" width="100"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="mobile" label="联系电话"></el-table-column>
      <el-table-column prop="salary" label="工资"></el-table-column>
      <el-table-column prop="entryDate" label="入职时间"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 对话框 -->
    <el-dialog title="员工编辑" :visible.sync="dialogFormVisible" width="500px">
        <el-form :model="pojo" ref="pojoForm" label-width="100px" label-posion="right" style="width: 400px" :rules="rules">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="pojo.username"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
                <el-input v-model="pojo.name"></el-input>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
                <el-input v-model="pojo.age"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="mobile">
                <el-input v-model="pojo.mobile"></el-input>
            </el-form-item>
            <el-form-item label="工资" prop="salary">
                <el-input v-model="pojo.salary"></el-input>
            </el-form-item>
            <el-form-item label="入职时间" prop="entryDate">
                <el-date-picker
                v-model="pojo.entryDate"
                type="date"
                value-format="yyyy-mm-dd"
                placeholder="选择日期"
                ></el-date-picker>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="pojo.id === null ? addData('pojoForm') : update('pojoForm')">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import staffApi from "@/api/staff";

export default {
  data() {
    return {
        list: [],
        pageSize: 10,
        currentPage: 1,
        total: 0,
        searchMap: {
            username: '',
            name: ''
        },
        dialogFormVisible: false,
        pojo: {
            id: null,
            username: '',
            name: '',
            age: '',
            mobile: '',
            entryDate: '',
            salary: ''
        },
        rules: {
          username: [
              {required: true, message: '用户名不能为空', trigger: 'blur'}
          ],
          name: [
              {required: true, message: '姓名不能为空', trigger: 'blur'}
          ]
      }
    };
  },

  created() {
    this.feachData();
  },

  methods: {
    feachData() {
      staffApi.search(this.currentPage, this.pageSize, this.searchMap).then(response => {
          const resp = response.data
          this.list = resp.data.rows
          this.total = resp.data.total
      })
    },
    // 分页
    handleSizeChange(val){
        this.pageSize = val
        this.feachData()
    },
    handleCurrentChange(val){
        this.currentPage = val
        this.feachData()
    },
    // 重置
    resetForm(formName) {
        this.$refs[formName].resetFields();
    },
    // 新增员工
    handleAdd(pojo){
        this.dialogFormVisible = true
        this.$nextTick(() => {
            this.$refs['pojoForm'].resetFields()
        })
    },
    // 确认添加员工
    addData(formName){
        this.$refs[formName].validate(valid => {
            if(valid){
                staffApi.add(this.pojo).then(response => {
                    const resp = response.data
                    if(resp.flag){
                        console.log('添加成功')
                        this.dialogFormVisible = false
                    }else{
                        this.$message({
                            message: resp.message,
                            type: 'warning'
                        })
                    }
                })
            }else{
                return false
            }
        })
    },
    // 编辑员工信息
    handleEdit(id){
        this.handleAdd()
        staffApi.getById(id).then(response => {
            const resp = response.data
            if(resp.flag){
                this.pojo = resp.data
            }
        })
    },
    // 确认修改员工信息
    update(formName){
        this.$refs[formName].validate(valid => {
            if(valid){
                staffApi.update(this.pojo).then(response => {
                    const resp = response.data
                    if(resp.message){
                        console.log('修改成功')
                        this.feachData()
                        this.dialogFormVisible = false
                    }else{
                        this.$message({
                            message: resp.message,
                            type: 'warning'
                        })
                    }
                })
            }else{
                return false
            }
        })
    },
    // 删除员工
    handleDelete(id){
        this.$confirm('确认删除这条记录吗？', '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消'
        }).then(() => {
            console.log('确认')
            staffApi.deleteById(id).then(response => {
                const resp = response.data
                this.$message({
                    message: resp.message,
                    type: resp.flag ? 'success' : 'error'
                })
                if(resp.flag){
                    this.feachData()
                }
            })
        }).catch(() => {
            console.log('取消')
        })
    }
  }
};
</script>

<style scoped>
.demo-form-inline {
  margin-top: 20px;
}
</style>